package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.qk.entity.Course;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
@SuppressWarnings("all")
public class CourseController {

    @Autowired
    CourseService courseService;
    @GetMapping
    public Result listCourses(CourseDTO courseDto){
        PageResult pageResult = courseService.listCourses(courseDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable("id") Integer id){
        courseService.deleteCourse(id);
        return Result.success();
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @GetMapping("/list")
    public Result getAllCourses(){
        List<Course> courses = courseService.getAllCourses();
        return Result.success(courses);
    }

    @GetMapping("/subject/{subject}")
    public Result getCoursesBySubject(@PathVariable("subject") String subject){
        List<Course> courses = courseService.getBySubject(subject);
        return Result.success(courses);
    }
}
